﻿using QLBH.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QLBH.DATA
{
    public class PhieuXuatData
    {
        private QLBHEntities db = new QLBHEntities();
        public string Error { set; get; }
        public string Result { set; get; }

        public List<PHIEU_XUAT> TimPhieuXuatTheoMa(string ma_phieu_xuat, int tong_so_dong)
        {
            db = new QLBHEntities();
            List<PHIEU_XUAT> list = new List<PHIEU_XUAT>();
            if (ma_phieu_xuat == "")
            {
                list = db.PHIEU_XUAT.ToList().Skip(tong_so_dong).Take(200).ToList().Select(p => new
                    PHIEU_XUAT()
                {
                    MA_PHIEU_XUAT = p.MA_PHIEU_XUAT,
                    NGAY_XUAT = p.NGAY_XUAT,
                    KHO_HANG = new KHO_HANG()
                    {
                        MA_KHO_HANG = p.KHO_HANG.MA_KHO_HANG,
                        TEN_KHO_HANG = p.KHO_HANG.TEN_KHO_HANG
                    }
                }).ToList();
            }
            else
                list = db.PHIEU_XUAT.Where(p => p.MA_PHIEU_XUAT.Contains(ma_phieu_xuat)).ToList().Skip(tong_so_dong).Take(200).ToList().Select(p => new
                    PHIEU_XUAT()
                {
                    MA_PHIEU_XUAT = p.MA_PHIEU_XUAT,
                    NGAY_XUAT = p.NGAY_XUAT,
                    KHO_HANG = new KHO_HANG()
                    {
                        MA_KHO_HANG = p.KHO_HANG.MA_KHO_HANG,
                        TEN_KHO_HANG = p.KHO_HANG.TEN_KHO_HANG
                    }
                }).ToList();
            return list;
        }

        public List<PHIEU_XUAT> TimPhieuXuatTheoMa(string ma_phieu_xuat)
        {
            db = new QLBHEntities();
            List<PHIEU_XUAT> list = new List<PHIEU_XUAT>();
            list = db.PHIEU_XUAT.Where(p => p.MA_PHIEU_XUAT == ma_phieu_xuat).ToList();
            return list;
        }

        private string TaoMa()
        {
            string result = "";
            if (db.PHIEU_XUAT.ToList().Count <= 0)
            {
                result = "0001";
            }
            else
            {
                int maxId = 0;

                foreach (var item in db.PHIEU_XUAT.ToList())
                {
                    if (maxId < int.Parse(item.MA_PHIEU_XUAT.Substring(2)))
                    {
                        maxId = int.Parse(item.MA_PHIEU_XUAT.Substring(2));
                    }
                }
                switch (maxId.ToString().Length)
                {
                    case 1:
                        result = "000" + (maxId + 1);
                        break;


                    case 2:
                        result = "00" + (maxId + 1);
                        break;


                    case 3:
                        result = "0" + (maxId + 1);
                        break;


                    case 4:
                        result = "" + (maxId + 1);
                        break;
                    default:
                        break;
                }
            }
            return "PX" + result;
        }

        bool KetChuyen(string MA_SAN_PHAM, string MA_KHO_HANG, DateTime NGAY)
        {
            try
            {
                var tonkho = db.TON_KHO.ToList().Where(p => p.MA_SAN_PHAM == MA_SAN_PHAM && p.MA_KHO_HANG == MA_KHO_HANG && p.NGAY_TON_KHO.Date == NGAY.Date).FirstOrDefault();
                if (tonkho == null)
                {
                    var toncu = db.TON_KHO.ToList().Where(p => p.MA_KHO_HANG == MA_KHO_HANG && p.MA_SAN_PHAM == MA_SAN_PHAM).OrderByDescending(p => p.NGAY_TON_KHO.Date).FirstOrDefault();
                    if (toncu != null)
                    {
                        TON_KHO T = new TON_KHO();
                        T.TON_DAU_KY = toncu.TON_CUOI_KY;
                        T.MA_SAN_PHAM = MA_SAN_PHAM;
                        T.MA_KHO_HANG = MA_KHO_HANG;
                        T.NGAY_TON_KHO = NGAY;
                        T.NHAP_TRONG_KY = 0;
                        T.XUAT_TRONG_KY = 0;
                        T.TONG_GT_NHAP = 0;
                        T.BAO_HANH = 0;
                        T.TON_CUOI_KY = T.TON_DAU_KY + T.NHAP_TRONG_KY - T.XUAT_TRONG_KY;
                        T.TONG_GT_NHAP += toncu.TONG_GT_NHAP;
                        T.DON_GIA_BINH_QUAN = toncu.DON_GIA_BINH_QUAN;
                        db.TON_KHO.Add(T);
                        db.SaveChanges();
                        return true;
                    }
                }
            }
            catch (Exception ex)
            {
                Error = ex.Message + " " + ex.InnerException.Message;
               // Error = "Kết chuyển thất bại ";
                return false;
            }
            return true;
        }

        public bool ThemMoi(PHIEUXUATPARAM PHIEUXUAT)
        {
            db = new QLBHEntities();
            try
            {
                PHIEU_XUAT pb = new PHIEU_XUAT();
                pb.MA_PHIEU_XUAT = PHIEUXUAT.MA_PHIEU_XUAT;
                pb.MA_KHACH_HANG = PHIEUXUAT.MA_KHACH_HANG;
                pb.MA_KHO_HANG = PHIEUXUAT.MA_KHO_HANG;
                pb.MA_NHAN_VIEN = PHIEUXUAT.MA_NHAN_VIEN;
                pb.NGAY_XUAT = DateTime.Now.Date;
                pb.SO_CHUNG_TU_GOC = PHIEUXUAT.SO_CHUNG_TU_GOC;
                pb.TAI_KHOAN_NO = PHIEUXUAT.TAI_KHOAN_NO;
                pb.TAI_KHOAN_CO = PHIEUXUAT.TAI_KHOAN_CO;
                pb.GIAM_GIA = PHIEUXUAT.GIAM_GIA;
                pb.VAT = PHIEUXUAT.VAT;
                pb.DA_TRA = PHIEUXUAT.DA_TRA;
                pb.SO_TIEN_NO = PHIEUXUAT.SO_TIEN_NO;
                pb.MO_TA = PHIEUXUAT.MO_TA;

                if (pb.MA_PHIEU_XUAT.Trim().Length <= 0)
                {
                    pb.MA_PHIEU_XUAT = TaoMa();
                }
                else if (db.PHIEU_XUAT.Any(p => p.MA_PHIEU_XUAT == pb.MA_PHIEU_XUAT))
                {
                    Error = "Mã Phiếu nhập đã tồn tại";
                    return false;
                }

                foreach (var item in PHIEUXUAT.CHITIETXUAT)
                {
                    if (!KetChuyen(item.MA_SAN_PHAM, PHIEUXUAT.MA_KHO_HANG, PHIEUXUAT.NGAY_XUAT))
                    {
                        Error += "Kết chuyển thất bại ";
                        return false;
                    }
                    CHI_TIET_XUAT CT = new CHI_TIET_XUAT();
                    CT.GIA_XUAT = item.GIA_XUAT;
                    CT.SO_LUONG_XUAT = item.SO_LUONG_XUAT;
                    CT.MA_PHIEU_XUAT = PHIEUXUAT.MA_PHIEU_XUAT;
                    CT.MA_SAN_PHAM = item.MA_SAN_PHAM;

                    var tonkho = db.TON_KHO.ToList().Where(p => p.TON_CUOI_KY >= item.SO_LUONG_XUAT && p.MA_SAN_PHAM == item.MA_SAN_PHAM && p.MA_KHO_HANG == PHIEUXUAT.MA_KHO_HANG && p.NGAY_TON_KHO.Date == PHIEUXUAT.NGAY_XUAT.Date).FirstOrDefault();

                    if (tonkho == null)
                    {
                        var sp = db.SAN_PHAM.ToList().Where(p => p.MA_SAN_PHAM == item.MA_SAN_PHAM).FirstOrDefault();
                        Error = sp.TEN_SAN_PHAM + " không đủ tồn kho";
                        return false;
                    }
                    CT.GIA_GOC = tonkho.DON_GIA_BINH_QUAN;
                    pb.CHI_TIET_XUAT.Add(CT);

                    tonkho.XUAT_TRONG_KY += CT.SO_LUONG_XUAT;
                    tonkho.TON_CUOI_KY -= CT.SO_LUONG_XUAT;
                    tonkho.TONG_GT_NHAP -= CT.SO_LUONG_XUAT * tonkho.DON_GIA_BINH_QUAN;
                }

                db.PHIEU_XUAT.Add(pb);
                db.SaveChanges();
                Result = pb.MA_PHIEU_XUAT;
                return true;
            }
            catch (Exception ex)
            {
                Error = ex.Message + " " + ex.InnerException.Message;
                return false;
            }
        }
    }
}